<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/word-break/
      思路：https://leetcode-cn.com/problems/word-break/solution/javascript-dong-tai-gui-hua-by-guyuejiajie/
    */
    let  s = "leetcode", wordDict = ["leet", "code"];

    function wordBreak(s, wordDict) {
      let dp = new Array(s.length).fill(false);
      let set = new Set(wordDict);

      for (let i = 0; i < s.length; i++) {
        if (set.has(s.slice(0, i+1))) {
          dp[i] = true;
          continue;
        }
        for (let j = 0; j < i; j++) {
          if (dp[j] && set.has(s.slice(j+1, i+1))) {
            dp[i] = true;
            break;
          }
        }
      }

      return dp[s.length-1];
    }
    console.log(wordBreak(s, wordDict));
  </script>
</body>
</html>